﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BrightIdeasSoftware;
using ROrganizer.UI.Controllers;
using ROrganizer.UI.Forms.Core;

namespace ROrganizer.UI.Forms
{
    internal partial class CrawlerForm : Form
    {
        private readonly CrawlerFormController controller;

        public CrawlerForm()
        {
            this.controller = new CrawlerFormController();
            this.InitializeComponent();

            this.olvLogs.ShowItemToolTips = true;
            this.olvLogs.CellToolTipGetter = (CellToolTipGetterDelegate)((column, model) => ((CrawlerFormController.Message)model).Content );

            this.controller.Changed += this.OnStateChanged;
            this.controller.Messages.CollectionChanged += this.OnChanged;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.olvLogs.Normalize();

            this.tbIndex.Text = this.controller.Index;
            this.tbCount.Text = this.controller.Count;

            this.RefreshActions();
        }

        private void OnChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (this.SynchronizeInvocation(this.OnChanged, sender, e))
            {
                if (e.Action == NotifyCollectionChangedAction.Reset)
                    this.olvLogs.ClearObjects();

                if (e.OldItems != null && e.OldItems.Count > 0)
                    this.olvLogs.RemoveObjects(e.OldItems);

                if (e.NewItems != null && e.NewItems.Count > 0)
                {
                    this.olvLogs.AddObjects(e.NewItems);

                    if (this.cbAutoscroll.Checked)
                        this.olvLogs.EnsureModelVisible(e.NewItems[e.NewItems.Count - 1]);
                }
            }
        }

        private void OnStateChanged(object sender, EventArgs e)
        {
            if (this.SynchronizeInvocation(this.OnStateChanged, sender, e))
                this.RefreshActions();
        }

        private void OnStart(object sender, EventArgs e)
        {
            this.controller.Start(this);
            this.RefreshActions();
        }

        private void OnStop(object sender, EventArgs e)
        {
            this.controller.Stop();
            this.RefreshActions();
        }

        private void OnUrlChanged(object sender, EventArgs e)
        {
            this.controller.Url = this.tbUrl.Text;
            this.RefreshActions();
        }

        private void OnIndexChanged(object sender, EventArgs e)
        {
            this.controller.Index = this.tbIndex.Text;
            this.RefreshActions();
        }

        private void OnCountChanged(object sender, EventArgs e)
        {
            this.controller.Count = this.tbCount.Text;
            this.RefreshActions();
        }

        private void OnAbout(object sender, EventArgs e)
        {
            this.ExecuteAndWatch(this.controller.ShowAbout, this);
        }

        private void OnResults(object sender, EventArgs e)
        {
            this.ExecuteAndWatch(this.controller.ShowResults, this);
        }

        private void OnSettings(object sender, EventArgs e)
        {
            this.ExecuteAndWatch(this.controller.ShowSettings, this);
        }

        private void RefreshActions()
        {
            this.btnStart.Enabled = this.controller.CanStart();
            this.btnStop.Enabled = this.controller.CanStop();
        }
    }
}
